@model PagedResult<UserListViewModel>
@using System.Linq
@{
    ViewData["Title"] = "用户列表";
}

<h2>用户列表</h2>

<form asp-action="Index" method="get" class="row g-3 mb-4">
    <div class="col-md-3">
        <input type="text" name="employeeNumber" class="form-control" placeholder="工号" value="@Context.Request.Query["employeeNumber"]" />
    </div>
    <div class="col-md-3">
        <input type="text" name="name" class="form-control" placeholder="姓名" value="@Context.Request.Query["name"]" />
    </div>
    <div class="col-md-6 d-flex align-items-end">
        <button type="submit" class="btn btn-primary me-2">查询</button>
        <a asp-action="Create" class="btn btn-success">添加用户</a>
    </div>
</form>

<table class="table table-bordered table-striped table-hover">
    <thead>
        <tr>
            <th>姓名</th>
            <th>工号</th>
            <th>电话</th>
            <th>邮箱</th>
            <th>性别</th>
            <th>出生日期</th>
            <th>户籍地</th>
            <th>所属组织</th>
            <th>角色</th>
            <th>更新时间</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.Items)
        {
            <tr>
                <td>@item.Name</td>
                <td>@item.EmployeeNumber</td>
                <td>@item.Phone</td>
                <td>@item.Email</td>
                <td>@(item.Gender == 1 ? "男" : item.Gender == 2 ? "女" : "未知")</td>
                <td>@item.BirthDate?.ToString("yyyy-MM-dd")</td>
                <td>@item.Hometown</td>
                <td>@item.OrganizationName</td>
                <td>@(item.Roles != null ? string.Join(", ", item.Roles.Select(r => r.Name)) : "")</td>
                <td>@item.UpdateTime.ToString("yyyy-MM-dd HH:mm")</td>
                <td>
                    <a asp-action="Details" asp-route-id="@item.Id" class="btn btn-info btn-sm">详情</a>
                    <a asp-action="Edit" asp-route-id="@item.Id" class="btn btn-warning btn-sm">编辑</a>
                </td>
            </tr>
        }
    </tbody>
</table>

@{
    var prevDisabled = Model.PageIndex <= 1 ? "disabled" : "";
    var nextDisabled = Model.PageIndex >= Math.Ceiling((double)Model.TotalCount / Model.PageSize) ? "disabled" : "";
    var totalPages = Math.Max(1, (int)Math.Ceiling((double)Model.TotalCount / Model.PageSize));
}

<nav aria-label="Page navigation">
    <ul class="pagination">
        <li class="page-item @prevDisabled">
            <a class="page-link" href="@Url.Action("Index", new { pageIndex = 1, pageSize = Model.PageSize, employeeNumber = Context.Request.Query["employeeNumber"], name = Context.Request.Query["name"] })">首页</a>
        </li>
        <li class="page-item @prevDisabled">
            <a class="page-link" href="@Url.Action("Index", new { pageIndex = Model.PageIndex - 1, pageSize = Model.PageSize, employeeNumber = Context.Request.Query["employeeNumber"], name = Context.Request.Query["name"] })">上一页</a>
        </li>
        
        @for (int i = Math.Max(1, Model.PageIndex - 2); i <= Math.Min(totalPages, Model.PageIndex + 2); i++)
        {
            <li class="page-item @(i == Model.PageIndex ? "active" : "")">
                <a class="page-link" href="@Url.Action("Index", new { pageIndex = i, pageSize = Model.PageSize, employeeNumber = Context.Request.Query["employeeNumber"], name = Context.Request.Query["name"] })">@i</a>
            </li>
        }
        
        <li class="page-item @nextDisabled">
            <a class="page-link" href="@Url.Action("Index", new { pageIndex = Model.PageIndex + 1, pageSize = Model.PageSize, employeeNumber = Context.Request.Query["employeeNumber"], name = Context.Request.Query["name"] })">下一页</a>
        </li>
        <li class="page-item @nextDisabled">
            <a class="page-link" href="@Url.Action("Index", new { pageIndex = totalPages, pageSize = Model.PageSize, employeeNumber = Context.Request.Query["employeeNumber"], name = Context.Request.Query["name"] })">末页</a>
        </li>
    </ul>
</nav>

<div class="text-muted">
    显示 @(((Model.PageIndex - 1) * Model.PageSize) + 1) 到 @(Math.Min(Model.PageIndex * Model.PageSize, Model.TotalCount)) 条，共 @Model.TotalCount 条记录
</div>
